#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "fileformat.h"
#include "scene.h"

/* Output data as PPM file */
void saveppm(char *filename, unsigned char *img){
	
	/* FILE pointer */
	FILE *f;
	
	/* Open file for writing 
	 * FILE *fopen(const char *path, const char *mode);
	 * fopen opens a file with "filename", and mode "w". 
	 * Check man fopen for list of modes. "w" is write, also overwrites existing files with the name "filename"
	 * Returns a FILE pointer when succesfull, otherwise returns NULL and global errno is used to indicate the error.
	*/
	f = fopen(filename, "w");
	
	/* PPM header info, including the size of the image 
	 * int fprintf(FILE *stream, const char *format, ...);
	 * Takes a file pointer as first argument, which in this case was created by fopen above.
	 * format part is used to determine fileformat and file header, this case the format is P6 and the rest are details of the format.
	 * Returns the number of characters printed.
	*/
	fprintf(f, "P6 %d %d %d\n", WIDTH, HEIGHT, 255);
	
	/* Write the image data to the file - remember 3 byte per pixel 
	 * size_t   fwrite(const  void  *ptr,  size_t  size,  size_t  nmemb,  FILE *stream);
	 * 1st argument: location pointer, 2nd argument: 3bytes per element, 3rd argument: number of elements, 4th argument: pointer to the input stream
	 * Returns the number of elements written.
	*/ 
	fwrite(img, 3, WIDTH*HEIGHT, f);
	
	/* Make sure you close the file 
	 * int fclose(FILE *fp);
	 * flushes the stream indicated by the FILE pointer *fp.
	 * returns 0 if the flush was succesfull, otherwise EOF is returned and errno is set to indicate the error. 
	*/
	fclose(f);

}
unsigned char *filedata;

char *savebmp_mem(unsigned char *img){
        
        int filesize = 54 + 3*WIDTH*HEIGHT;
        if(filedata){ free(filedata); }
        filedata = malloc(filesize);
        
        unsigned char bmpfileheader[14] = {'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0};
        unsigned char bmpinfoheader[40] = {40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0};;
       
        /* Construct header with filesize part */
        bmpfileheader[ 2] = (unsigned char)(filesize    );
        bmpfileheader[ 3] = (unsigned char)(filesize>> 8);
        bmpfileheader[ 4] = (unsigned char)(filesize>>16);
        bmpfileheader[ 5] = (unsigned char)(filesize>>24);
        
        /* Construct header with width and height part */
        bmpinfoheader[ 4] = (unsigned char)(       WIDTH    );
        bmpinfoheader[ 5] = (unsigned char)(       WIDTH>> 8);
        bmpinfoheader[ 6] = (unsigned char)(       WIDTH>>16);
        bmpinfoheader[ 7] = (unsigned char)(       WIDTH>>24);
        bmpinfoheader[ 8] = (unsigned char)(       HEIGHT    );
        bmpinfoheader[ 9] = (unsigned char)(       HEIGHT>> 8);
        bmpinfoheader[10] = (unsigned char)(       HEIGHT>>16);
        bmpinfoheader[11] = (unsigned char)(       HEIGHT>>24);
        
		memcpy(filedata, bmpfileheader, 14);
		memcpy(filedata+14, bmpinfoheader, 40);
		memcpy(filedata+54, img, filesize-54);
		
return filedata;
}

void savebmp(char *filename, unsigned char *img){
        int i;
        FILE *f;
        int filesize = 54 + 3*WIDTH*HEIGHT;
        
        unsigned char bmpfileheader[14] = {'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0};
        unsigned char bmpinfoheader[40] = {40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0};
        unsigned char bmppad[3] = {0,0,0};
       
        /* Construct header with filesize part */
        bmpfileheader[ 2] = (unsigned char)(filesize    );
        bmpfileheader[ 3] = (unsigned char)(filesize>> 8);
        bmpfileheader[ 4] = (unsigned char)(filesize>>16);
        bmpfileheader[ 5] = (unsigned char)(filesize>>24);
        
        /* Construct header with width and height part */
        bmpinfoheader[ 4] = (unsigned char)(       WIDTH    );
        bmpinfoheader[ 5] = (unsigned char)(       WIDTH>> 8);
        bmpinfoheader[ 6] = (unsigned char)(       WIDTH>>16);
        bmpinfoheader[ 7] = (unsigned char)(       WIDTH>>24);
        bmpinfoheader[ 8] = (unsigned char)(       HEIGHT    );
        bmpinfoheader[ 9] = (unsigned char)(       HEIGHT>> 8);
        bmpinfoheader[10] = (unsigned char)(       HEIGHT>>16);
        bmpinfoheader[11] = (unsigned char)(       HEIGHT>>24);
        
        f = fopen(filename,"wb");
        fwrite(bmpfileheader,1,14,f);
        fwrite(bmpinfoheader,1,40,f);
        
       
        for(i=0; i<HEIGHT; i++){
                fwrite(img+(WIDTH*(i)*3),3,WIDTH,f);
                fwrite(bmppad,1,(4-(WIDTH*3)%4)%4,f);
		
        }
                
        fclose(f);
}
